
define(['ionic', 'controllers'], function () {
    angular.module('U9Mobile.controllers')
        .controller('SettingCtrl', ['$scope', 'AppManager', 'UtilService', 'AppConstants','Push','$ionicLoading','$cordovaToast','$ionicPlatform',
            function ($scope, AppManager, UtilService, AppConstants, Push,$ionicLoading,$cordovaToast,$ionicPlatform) {
                $scope.data = {
                    version: AppConstants.APP_VERSION,
                    pubTime: AppConstants.APP_PUBTIME,
                    isIOS: ionic.Platform.isIOS(),
                    msgExpand: false,
                    PushStop:false
                };
                $scope.clearBadge=function(){
                    if(!$scope.data.PushStop){
                        $ionicLoading.show({duration:500})
                        if(window.cordova){
                            window.plugins.jPushPlugin.setBadge(0);
                            window.plugins.jPushPlugin.setApplicationIconBadgeNumber(0);
                        }
                    }
                }
                function checkPushStop(){
                    window.plugins.jPushPlugin.isPushStopped(function(data){
                        if(data>0){
                            $scope.$apply(function(){
                                $scope.data.PushStop=true;
                            })
                        }else{
                            $scope.$apply(function(){
                                $scope.data.PushStop=false;
                            })
                        }
                    })
                }
                if(window.cordova){
                    checkPushStop();
                }
                $ionicPlatform.ready(function(){
                    $ionicPlatform.on('resume',function(){
                        checkPushStop();
                    })
                })

                //$scope.data.msg = Push.settingList.msg;

                //if(!$scope.data.isIOS){
                    $ionicLoading.show({noBackdrop:true});
                    Push.initOptSetting().then(function(setting){
                        console.log(setting);
                        $ionicLoading.hide();
                        if(typeof setting=='boolean'){
                            $cordovaToast.showShortCenter("请先登陆账号");
                        }else{
                            $scope.data.msg = setting.msg;
                            $scope.data.notify=setting.notify;
                            $scope.data.mute = setting.mute;
                            $scope.data.GlobalSwitch=setting.GlobalSwitch;
                            $scope.data.msgExpand=setting.GlobalSwitch.IsOpen;
                        }

                    },function(setting){
                        $ionicLoading.hide();
                        console.log("error")
                        $scope.data.msg={};
                        $scope.data.notify=setting.GlobalSwitch;
                        $scope.data.mute = setting.mute;
                    })
                //}

                $scope.switchNote = function(data) {
                    console.log(data);
                    Push.SyncService(data).then(function(t){
                        data.IsOpen=t;
                    },function(t){
                        data.IsOpen=t;
                    });
                };
                $scope.switchMute = function(data) {
                    Push.setMute($scope.data.mute.IsOpen,$scope.data.GlobalSwitch.IsOpen).then(function(t){
                        $scope.data.msgExpand=$scope.data.GlobalSwitch.IsOpen;
                    },function(t){
                        console.log(typeof t);
                        data.IsOpen=!data.IsOpen;
                        //$scope.data.msgExpand=t;
                    });
                };
                $scope.checkUpdate = function() {
                    UtilService.showLoading();
                    AppManager.checkAppUpdate().finally(function () {
                        UtilService.hideLoading();
                    });
                };
                $scope.expandMsgSetting = function() {
                    Push.isPushStopped();
                };
            }
        ]);
});